package dwz.common.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class FullSortUtils {
	
	    //将NUM设置为待排列数组的长度即实现全排列  
	    private  int NUM = 4;  
	    private  Set<String> set = new HashSet<String>();
	    private  String[] haomas;
	    public FullSortUtils(String[] haStrings){
	    	this.haomas = haomas;
	    	sort(Arrays.asList(haStrings), new ArrayList<String>()); 
	    }
	    /** 
	     * 递归算法：将数据分为两部分，递归将数据从左侧移右侧实现全排列 
	     * 
	     * @param datas 
	     * @param target 
	     */  
	    private  void sort(List<String> datas, List<String> target) {  
	        if (target.size() == NUM) {  
	        	StringBuilder haoma = new StringBuilder();
	            for (Object obj : target){
	            	System.out.print(obj);  
	            	haoma.append(obj.toString());
	            }
	            set.add(haoma.toString());  
	            System.out.println();  
	            return;  
	        }  
	        for (int i = 0; i < datas.size(); i++) {  
	            List<String> newDatas = new ArrayList<String>(datas);  
	            List<String> newTarget = new ArrayList<String>(target);  
	            newTarget.add(newDatas.get(i));  
	            newDatas.remove(i);  
	            sort(newDatas, newTarget);  
	        }  
	    }  
	  
	    public static void main(String[] args) {  
	        String[] datas = new String[] { "a", "b", "c", "d" };  
	        FullSortUtils fullSortUtils = new FullSortUtils(datas);
	        System.out.println(fullSortUtils.getHaoMa().length);
	    }
	
	  public String[] getHaoMa(){
		  String[] hms = new String[set.size()];
		  Iterator<String> iterator = set.iterator();
		  int i = 0;
		  while(iterator.hasNext()){
			  hms[i++] = iterator.next();
		  }
		  System.out.println(hms.length);
		  return hms;
	  }
	  
}
